import security from "@/apis/security";
import { clearJump } from '../utils/navigator';
import { message } from 'antd';

const SecurityModel = {
  state: {
    needLogin: false,
    isFirst: true,
  },
  effects: (dispatch) => ({
    async login(payload, state) {
      const data = await security.login(payload);
      if (data) {
        await dispatch.global.save({
          user: data
        });
        await dispatch.security.save({
          needLogin: false,
          isFirst: false,
        });
        clearJump('/');
      }
    },
    async register(payload, state) {
      const data = await security.register(payload);
      if (data) {
        clearJump('/login');
      }
    },
    async forgetPwd(payload, state) {
    },
    async emailCode(payload, state) {
      const data = await security.code(payload);
      if (data) {
        message.success('验证码发送成功');
      }
    },
    async exit(payload, state) {
      const data = await security.exit();
      if (data) {
        clearJump('/login');
      }
    },
    async findPwd(payload, state) {
      const data = await security.findPwd(payload);
      if (data) {
        message.success(data);
        clearJump('/login');
      }
    }
  }),
  reducers: {
    save(prevState, payload) {
      return {
        ...prevState,
        ...payload
      };
    }
  },
}

export default SecurityModel;